<template>
	<view v-if="load">
		<view class="one-time-box">
			<view class="form-title" >
				<view class="form-label">批单生效日期</view>
				<view class="form-content" >
					<view class="form-times" @tap="dateOpen('hopeBeginTime', hopeBeginTime)" >
						<text v-if="start_time">{{start_time}}</text>
						<text v-else class="form-times-place">请选择生效日期</text>
					</view>
				</view>
				<image src="https://jy-yyzj.oss-cn-shanghai.aliyuncs.com/jdbx/h5/user-menu-right.png" mode="widthFix" class="form-right-icon"></image>
			</view>
		</view>
		<view class="title-box" v-if="user.length > 0">
			<view class="title-line-box"></view>
			<view class="title-text-box">
				批改人员
			</view>
		</view>
		<view class="user-list-box" v-if="user.length > 0">
			<view class="user-item-box" v-for="(item,key) in user" :key="key">
				<view class="info-user-box" v-if="item.id">
					<view class="info-left-box">
						<span>原姓名</span>
						<span class="info-value-box">{{item.name}}</span>
					</view>
					<view class="info-right-box">
						<span>原身份证号</span>
						<span  class="info-value-box">{{item.id_card}}</span>
					</view>
				</view>
				<view class="info-user-box">
					<view class="input-left-box">
						<view calss="title-name-box">姓名</view>
						<view class="input-name-value-box">
							<input placeholder="请输入" class="form-input" v-model="item.new_name" placeholder-style="color:#aaaaaa;font-size:26upx" >
						</view>
					</view>
					<view class="input-right-box">
						<view class="title-card-box">身份证号</view>
						<view class="input-card-value-box">
							<input placeholder="请输入" class="form-input" v-model="item.new_card" placeholder-style="color:#aaaaaa;font-size:26upx" >
						</view>
					</view>
				</view>
				<view class="del-box">
					<view class="del-info-box">
						<view class="del-img-box">
							<image src="https://jy-shops.oss-cn-beijing.aliyuncs.com/images/icon/del-icon.png" mode="widthFix" class="del-image"></image>
						</view>
						<view class="del-text-box" @click="delUser(key)">
							删除
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add-btn-box" @click="addUser()" v-if="dec_num == 0">
			添加人员
		</view>
		<my-datetime ref="dateTimePop" @ok="timeOk" :shownum="3"></my-datetime>
		<view class="footer-box">
			<view class="order-time-box">
				保单起止时间：{{"2020-02-02 20:20:20~2020-02-02 20:20:20"}}
			</view>
			<view class="footer-btn-box">
				<view class="footer-btn-left-box">
					新增{{inc_num - dec_num}}人，合计保费<span class="price-text-span">￥{{total_price}}</span>
				</view>
				<view class="footer-btn-right-box" @click="submitDo()">
					确认批改
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import myDatetime from '@/components/my-datetime/my-datetime';
	export default{
		components:{ myDatetime },
		data(){
			return {
				id : 0,
				ids:[],
				load : false,
				user:[],
				start_time:'',
				hopeBeginTime: '',
				dec_num:0,
				inc_num:0,
				total_price:0,
				price:0,
				typeL:'',
			}
		},
		onLoad(e) {
			this.id = e.id;
			this.typeL=e.type
			console.log(e.ids);
			this.ids = JSON.parse(e.ids);
			console.log(this.ids);
			this.getData();
		},
		methods:{
			//选择开始日期
			timeOk(e){
				var start_time = e+' 00:00:00';
				var timestamp = Date.parse(new Date());
				var starttime = this.getTargetTime(start_time);
				if(starttime < timestamp)return this.toast('生效时间不能小于当前时间');
				this.start_time = e+' 00:00:00';
				if(uni.getSystemInfoSync().platform == 'ios') {
					this.start_time = this.start_time.replace(/-/g,"/");
				}
				
				var d2=new Date(this.start_time); 
				d2.setFullYear(d2.getFullYear()+1); 
				d2.setDate(d2.getDate()); 
				var year = d2.getFullYear();
				var month = d2.getMonth() + 1;
				month = month < 10 ? '0'+month : month;
				var day = d2.getDate();
				day = day < 10 ? '0'+day : day;
				var hour = d2.getHours();
				hour = hour < 10 ? '0'+hour : hour;
				var minut = d2.getMinutes();
				minut = minut < 10 ? '0'+minut : minut;
				var second = d2.getSeconds();
				second = second < 10 ? '0'+second : second;
				var date = year+'-'+month+'-'+day;
				this.end_time = date + ' 00:00:00';
				if(uni.getSystemInfoSync().platform == 'ios') {
					this.end_time = this.end_time.replace(/-/g,"/");
				}
				this.ajax('user/getChangePrice',{
					data: {
					  id : this.id,
					  start_time:this.start_time
					},
					success:(res) => {
					  if(res.code){
						this.price = res.msg;
						this.totalPrice();
					  }else{
						this.toast(res.msg);
					  }
					}
				})
			},
			getTargetTime(t){
			    var d = t.split(" ")[0],
			        h = t.split(" ")[1],
			        date = new Date()
			
			    date.setYear(d.split("-")[0])
			    date.setMonth(d.split("-")[1] - 1)
			    date.setDate(d.split("-")[2])
			
			    date.setHours(h.split(":")[0])
			    date.setMinutes(h.split(":")[1])
			    date.setSeconds(h.split(":")[2])
			    
			    return date.getTime()    
			},
			// 开启弹窗
			dateOpen (key, date) {
				this.dateKey = key
				this.$refs.dateTimePop.open(date || '');
			},
			getData(){
				uni.showLoading({
					title:'加载中'
				})
				this.ajax('user/getChangeUser',{
					data:{
						ids: this.ids,
					},
					success:(res) => {
						uni.hideLoading();
						this.user = res.msg.user;
						console.log(this.user)
						//this.id = res.msg.id;
						this.dec_num = this.user.length;
						this.inc_num = this.user.length;
						this.total_price = 0;
						this.load = true;
					}
				});
			},
			addUser(){
				if(this.dec_num > 0)return this.toast('替换人员是不能添加人员');
				  this.user.push({
					name:'----',
					id_card:'----',
					new_name:'',
					new_card:'',
				  })
				  this.inc_num = this.user.length;
				  this.totalPrice();
			},
			delUser(index){
			      if(this.user[index]['id']){
			        this.dec_num--;
			      }
			      this.user.splice(index,1);
			      this.inc_num = this.user.length;
			      this.totalPrice();
			    },
			totalPrice(){
				this.total_price = ((this.inc_num - this.dec_num) * this.price).toFixed(2);
				console.log(this.price);
			},
			submitDo(){
				uni.showLoading({
					mask:true,
					title:"数据加载中"
				})
				if(this.user.length == 0)return this.toast('请添加批改人');
				this.ajax(this.typeL==1 ? 'user/changeOrderDo' : 'paorder/changeOrderDo',{
				        data: {
				          order_id : this.id,
				          user:this.user,
				          start_time:this.start_time
				        },
				        success:(res) => {
				          if(res.code){
				            uni.navigateTo({
				            	url:'/pages/order/endorse?id='+res.msg
				            })
				          }else{
				            this.toast(res.msg);
				          }
						  uni.hideLoading()
				        }
				      })
			},
		}
	}
</script>

<style>
	@import url("@/static/css/order/change.css");
</style>
